import socket,select,os
from base import *
internetEth = 'enp5s0'

def natIni(internetEth,natEth):    
    os.system('iptables -t nat -A POSTROUTING -o %s -j MASQUERADE'%internetEth)
    os.system('iptables -A FORWARD -i %s -o %s -m state --state RELATED,ESTABLISHED -j ACCEPT'%\
              (internetEth,natEth))
    os.system('iptables -A FORWARD -i %s -o %s -j ACCEPT'%(natEth,internetEth))
    os.system('echo 1 > /proc/sys/net/ipv4/ip_forward ')
    
os.system('ovs-vsctl del-br br1')
os.system('ip tuntap del dev tap1 mod tap')

os.system('ovs-vsctl add-br br1')
os.system('ip tuntap add dev tap1 mod tap')
os.system('ip link set tap1 up')   

os.system('ovs-vsctl add-port br1 tap1')
os.system('ovs-vsctl add-port br1 mydev -- set Interface mydev type=internal')   
os.system('ip link set mydev up')

os.system('ifconfig mydev 192.168.105.1 up')
natIni(internetEth,'mydev')

tap = openTap(b'tap1')
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
add = ('0.0.0.0',19988)
s.bind(add)
s.listen(1)
a,b = s.accept()
s.close()
a.setblocking(False)
work(a,tap)


